package net.mengkai.activiti7.deployer.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class UserInfoAuthenticationToken extends AbstractAuthenticationToken {

    private Object user;

    public Object getUser() {
        return user;
    }

    public void setUser(Object user) {
        this.user = user;
    }

    public UserInfoAuthenticationToken(Collection<? extends GrantedAuthority> authorities, Object user) {
        super(authorities);
        this.user = user;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        JSONObject userData = ((JSONObject) this.user);
        String username = userData.getString("name");
        if (username != null && !"".equals(username)) {
            return username;
        }
        return userData.getString("email");
    }
}
